package com.ChenYaRou.www.dao.impl;

import com.ChenYaRou.www.dao.DetailedUserDao;
import com.ChenYaRou.www.po.DetailedUser;
import com.ChenYaRou.www.utils.GenerateSql;
import com.ChenYaRou.www.utils.JdbcUtil;

import java.sql.SQLException;

/**
 * @ClassName DetailedUserDaoImpl
 * @Description TODO
 * @Author 86183
 * @Date 2022/4/29 23:03
 * @Version 1.0
 **/
public class DetailedUserDaoImpl implements DetailedUserDao {
    /**
     * 搜索
     * @param userId 用户的id
     * @return user的详细信息
     * @throws SQLException
     * @throws  NoSuchFieldException
     * @throws InstantiationException
     * @throws  IllegalAccessException
     */
    @Override
    public DetailedUser select(Integer userId) throws SQLException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        String sql="select * from detaileduser where userId="+userId;
        return JdbcUtil.getInstance().selectSimple(sql,DetailedUser.class);
    }

    /**
     * 插入
     * @param detailedUser detailedUser
     * @return 成功与否(>0成功 0 false)
     * @throws  IllegalAccessException
     * @throws SQLException
     */
    @Override
    public int insert(DetailedUser detailedUser) throws IllegalAccessException, SQLException {
        return JdbcUtil.getInstance().insertOrUpdate(GenerateSql.getInstance().insert(detailedUser));
    }

    /**
     * 更新user
     * @param newDetailedUser 存放更新值的user
     * @param oldDetailedUser 旧的detailUser 其实只需要用到其中的userId
     * @return 更新条数
     * @throws IllegalAccessException 非法访问
     * @throws SQLException sql异常
     */
    @Override
    public int update(DetailedUser newDetailedUser, DetailedUser oldDetailedUser)
            throws IllegalAccessException, SQLException {
        return JdbcUtil.getInstance().insertOrUpdate(GenerateSql.getInstance().update(newDetailedUser,oldDetailedUser));
    }
}
